<?php
class Settings{
	private static $set = null;

	private $settings = array();
	
	public static function getInstance() {
		if(self::$set == null) self::$set = new Settings();
		return self::$set;	
	}
	
	public function __construct() {
		// Get Settings
		$query = DB::getInstance()->query("SELECT setName,setValue FROM ".Tables::$settingsTbl."");
		while($s = DB::getInstance()->fetchArray($query)) {
			$this->settings[$s['setName']] = $s['setValue'];
		}
	
		$query = DB::getInstance()->query("SELECT setName,setValue FROM ".Tables::$settingsTextTbl."");
		while($s = DB::getInstance()->fetchArray($query)) {
			$this->settings[$s['setName']] = $s['setValue'];
		}
	}
	
	public function getSetting($name) {
		return $this->settings[$name];
	}
	
	public function addSetting($name,$value) {
		$this->settings[$name] = $value;
	}
	
	public function createSetting($setName,$setValue) {
		$value = (is_numeric($setValue)) ? $setValue:DB::parseVar($setValue);
		DB::getInstance()->query("
			INSERT INTO
				".Tables::$settingsTbl."
			SET
				setName = '".DB::parseVar($setName)."',
				setValue = '".$value."'");
	}
	
	public function createSettingText($setName,$setValue) {
		$value = (is_numeric($setValue)) ? $setValue:DB::parseVar($setValue);
		DB::getInstance()->query("
			INSERT INTO
				".Tables::$settingsTextTbl."
			SET
				setName = '".DB::parseVar($setName)."',
				setValue = '".$value."'");
	}
	
	public function deleteSetting($setName) {
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$settingsTbl."
			WHERE
				setName = '".$setName."'
				");
	}
	
	public function deleteSettingText($setName) {
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$settingsTextTbl."
			WHERE
				setName = '".$setName."'
				");
	}
	
	public function changeSetting($setName,$newValue) {
		DB::getInstance()->query("
			UPDATE
				".Tables::$settingsTbl."
			SET
				setValue = '".DB::parseVar($newValue)."'
			WHERE
				setName = '".DB::parseVar($setName)."'");
		$this->settings[$setName] = $newValue;
	}
	
	public function changeSettingText($setName,$newValue) {
		DB::getInstance()->query("
			UPDATE
				".Tables::$settingsTextTbl."
			SET
				setValue = '".DB::parseVar($newValue)."'
			WHERE
				setName = '".DB::parseVar($setName)."'");
		$this->settings[$setName] = $newValue;
	}
	
	public function assignSettingsToTpl() {
		$tempSettings = array();
		foreach($this->settings as $key=>$val) {
			$tempSettings[str_replace(".","",$key)] = $val;
		}
		General::getTpl()->assign("settings",$tempSettings);
	}
}
?>